package com.wzdigit.wms.hm.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class CodeGenerator {
    /**
     * <p>
     * 读取控制台内容
     * </p>
     */
    @SuppressWarnings({"resource", "deprecation"})
    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入" + tip + "：");
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotEmpty(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }

    public static void main(String[] args) {
        String tbName = scanner("表名");
        String EntityName = scanner("实体名");
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();
        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("");
        gc.setOpen(false);
        gc.setBaseResultMap(true);
        gc.setMapperName(EntityName + "Mapper");
        gc.setEntityName(EntityName);
        gc.setXmlName(EntityName + "Mapper");
        gc.setControllerName(EntityName + "Controller");
        mpg.setGlobalConfig(gc);
        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();

//        dsc.setUrl("jdbc:oracle:thin:@10.131.10.33:1521:mesdev");
//        dsc.setDriverName("oracle.jdbc.driver.OracleDriver");
//        dsc.setDbType(DbType.ORACLE);
//        dsc.setUsername("mesktmod");
//        dsc.setPassword("meswz8094");

        //家用测试
        //WMS_GDN_DTL
        dsc.setUrl("jdbc:oracle:thin:@10.131.10.33:1521:mesdev");
        dsc.setDriverName("oracle.jdbc.driver.OracleDriver");
        dsc.setDbType(DbType.ORACLE);
        dsc.setUsername("mesktmod");
        dsc.setPassword("meswz8094");



        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
//        pc.setModuleName(scanner("模块名"));
        pc.setEntity("domain");
        pc.setParent(CodeGenerator.class.getPackage().getName().replace(".config", ""));
        mpg.setPackageInfo(pc);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setSuperEntityClass("com.wzdigit.framework.orm.model.BasicDomain");
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
//        strategy.setSuperControllerClass("com.wzdigit.mes.eqp.controller.BaseController");
        strategy.setSuperMapperClass("com.baomidou.mybatisplus.core.mapper.BaseMapper");
//        strategy.setSuperServiceImplClass("com.wzdigit.framework.orm.service.impl.CrudServiceImpl");
        strategy.setInclude(tbName);
//        strategy.setSuperEntityColumns("id");
        strategy.setControllerMappingHyphenStyle(true);
//        strategy.setTablePrefix(pc.getModuleName() + "_");
        mpg.setStrategy(strategy);

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };
        List<FileOutConfig> focList = new ArrayList<>();
        focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名称
                return projectPath + "/src/main/java/" + CodeGenerator.class.getPackage().getName().replace(".config", ".mapper.xml.").replace(".", "/") //+ pc.getModuleName() + "/"
                        + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);
        mpg.setTemplate(new TemplateConfig().setXml(null));

        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }

    static String getSubString(String s) {
        try {
            String bufferString = "";
            String lastString = "";
            int i = 0;
            while (true) {
                // 首次进入
                if (i == 0) {
                    if (s.indexOf("_") == -1) {
                        return s;
                    }
                    bufferString = s.substring(0, s.indexOf("_"));
                    lastString = s.substring(s.indexOf("_") + 1);
                    i++;
                }
                // 首字母大写
                bufferString += lastString.substring(0, 1).toUpperCase()
                        + (lastString.indexOf("_") == -1 ? lastString.substring(1)
                        : lastString.substring(1, lastString.indexOf("_")));
                if (lastString.indexOf("_") == -1) {
                    return bufferString;
                }
                lastString = lastString.substring(lastString.indexOf("_") + 1);
            }
        } catch (Exception e) {
            throw new Error("字段内容格式错误");
        }
    }
}
